package cc.rengu.igas.channel.upqc.core.service.trans;


import cc.rengu.igas.channel.upqc.common.enums.RespCodeEnum;
import cc.rengu.igas.channel.upqc.common.constant.UpqcParamConstant;
import cc.rengu.igas.channel.upqc.common.constant.UpqcTreeNodeConstant;
import cc.rengu.igas.channel.upqc.common.util.UpqcSecurityUtil;
import cc.rengu.igas.channel.upqc.core.model.ChnlBizResponse;
import cc.rengu.igas.channel.upqc.core.service.base.PayService;
import cc.rengu.igas.channel.upqc.facade.bean.ComInfo;
import cc.rengu.igas.channel.upqc.facade.bean.CouponInfoBean;
import cc.rengu.igas.channel.upqc.facade.bean.PayerInfo;
import cc.rengu.igas.channel.upqc.facade.request.C2BTranStateQueryRequest;
import cc.rengu.igas.channel.upqc.facade.response.C2BTranStateQueryResponse;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.entity.DstChannelInfo;
import cc.rengu.oltp.service.common.enums.OltpRpcdEnum;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.utility.util.*;
import com.alibaba.fastjson.JSON;
import org.apache.commons.codec.binary.Base64;

import java.util.List;
import java.util.Map;


/**
 * 收款方C2B交易查询
 * AC->UP
 * reqType:0350000903
 */
public class C2BTransStateQueryService extends PayService {

    @Override
    protected Object beforeBizProcess() throws Exception {
        C2BTranStateQueryRequest c2BTranStateQueryRequest = new C2BTranStateQueryRequest();
        ConvertUtil.convertOutput(c2BTranStateQueryRequest);
        return c2BTranStateQueryRequest;
    }

    @Override
    protected ChnlBizResponse callBizService(Object request) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        int iReturnCode = 0;
        DstChannelInfo dstChannelInfo = (DstChannelInfo) xmlTreeUtil.getXmlTreeObjectValue(UpqcTreeNodeConstant.DST_CHANNEL_INFO, DstChannelInfo.class);
        C2BTranStateQueryResponse c2BTranStateQueryResponse = new C2BTranStateQueryResponse();
        C2BTranStateQueryRequest c2BTranStateQueryRequest = (C2BTranStateQueryRequest) request;
        /* 根据微信支付产品类型对接口接口字段赋值 */
        ConvertUtil.convertInput(c2BTranStateQueryRequest);
        /* 根据通道标识调用不同通道进行支付 */
        xmlTreeUtil.setXmlTreeStringValue(UpqcTreeNodeConstant.txnType, c2BTranStateQueryRequest.getHeader().getTxnNum());
        xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.TXN_NUM, c2BTranStateQueryRequest.getHeader().getTxnNum());

        /* 演示环境发送短信默认返回成功 */
        if (AppParamConstant.DEFAULT_ENV.equals(AppParamConstant.APPLICATION_ENV)) {
            ChnlBizResponse chnlBizResponse = new ChnlBizResponse();
            c2BTranStateQueryResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
            c2BTranStateQueryResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
            c2BTranStateQueryResponse.setOrigRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
            c2BTranStateQueryResponse.setOrigRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
            c2BTranStateQueryResponse.setVoucherNum(RandomUtil.randomNumStr(32));
            c2BTranStateQueryResponse.setAuthCode(RandomUtil.getRandomInt());
            c2BTranStateQueryResponse.setSettleKey(c2BTranStateQueryResponse.getVoucherNum());
            c2BTranStateQueryResponse.setSettleDate(DateUtil.getCurrentDate());
            PayerInfo payerInfo = new PayerInfo();
            payerInfo.setAccNo("6210960000000000021");
            payerInfo.setAcctClass("00");
            c2BTranStateQueryResponse.setPayerInfo(payerInfo);
            chnlBizResponse.setResult(c2BTranStateQueryResponse);
            return chnlBizResponse;
        }

        iReturnCode = schedule.callService(dstChannelInfo.getDstServiceName(), 0);
        /* 恢复原交易码 */
        if (98 == iReturnCode) {
            rglog.error("调用银联二维码条码收款查询服务<{}>超时，iReturnCode:<{}>", dstChannelInfo.getDstServiceName(), iReturnCode);
            throw new BizException(RespCodeEnum.TIME_OUT_ERROR.getRespCode(), RespCodeEnum.TIME_OUT_ERROR.getRespDesc());
        } else if (0 != iReturnCode) {
            rglog.error("调用银联二维条码收款查询服务<{}>失败，iReturnCode:<{}>", dstChannelInfo.getDstServiceName(), iReturnCode);
            throw new BizException(OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespCode(), OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespDesc());
        }
        ConvertUtil.convertOutput(c2BTranStateQueryResponse);
        ChnlBizResponse chnlBizResponse = new ChnlBizResponse();
        if (UpqcParamConstant.SUCCESS_CODE.equals(c2BTranStateQueryResponse.getRespCode())) {
            /*银联返回通讯层是00*/
            c2BTranStateQueryResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
            c2BTranStateQueryResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        } else {
            /*银联返回通讯层交易是非00*/
            chnlBizResponse.setResult(c2BTranStateQueryResponse);
            return chnlBizResponse;
        }
        if (c2BTranStateQueryResponse.getSettleDate() != null) {
            c2BTranStateQueryResponse.setSettleDate(UpqcSecurityUtil.getCupsSettleDate(c2BTranStateQueryResponse.getSettleDate()));
        }
        String payInfoEnc = xmlTreeUtil.getXmlTreeStringValue(UpqcTreeNodeConstant.payInfoStr);

        if (payInfoEnc != null) {
            String payInfo = descryptData(payInfoEnc);
            rglog.error("解密的payInfo:<{}>", payInfo);
            c2BTranStateQueryResponse.setPayerInfo((PayerInfo) convertKvStr2Bean(payInfo.substring(1, payInfo.length() - 1), PayerInfo.class));
        }

        String comInfoStr = xmlTreeUtil.getXmlTreeStringValue(UpqcTreeNodeConstant.comInfoStr);
        if (comInfoStr != null) {
            comInfoStr = new String(Base64.decodeBase64(comInfoStr));
            rglog.error("解密的comInfo:<{}>", comInfoStr);
            ComInfo comInfo = new ComInfo();
            Map<String, Object> kvMap = UpqcSecurityUtil.parseKvString(comInfoStr.substring(1, comInfoStr.length() - 1));
            if (kvMap.get("F0") != null) {
                comInfo.setMsgType((String) kvMap.get("F0"));
            }
            if (kvMap.get("F3") != null) {
                comInfo.setProcCode((String) kvMap.get("F3"));
            }
            if (kvMap.get("F25") != null) {
                comInfo.setEntryMode((String) kvMap.get("F25"));
            }
            if (kvMap.get("F37") != null) {
                comInfo.setReferenceNo((String) kvMap.get("F37"));
            }
            if (kvMap.get("F60") != null) {
                comInfo.setReserve((String) kvMap.get("F60"));
            }
            c2BTranStateQueryResponse.setComInfo(comInfo);
        }
        chnlBizResponse.setResult(c2BTranStateQueryResponse);
        return chnlBizResponse;
    }

    @Override
    protected void afterBizProcess(ChnlBizResponse response) throws Exception {
        C2BTranStateQueryResponse c2BTranStateQueryResponse = (C2BTranStateQueryResponse) response.getResult();
        ConvertUtil.convertInput(c2BTranStateQueryResponse);
    }
}
